<script>
    var valid_result = false
    {notempty name="row"}
    var row = {:json_encode($row, 256)};
    {else/}
    var row = {};
    {/notempty}

    var app = new Vue({
        el: '#app',
        data: {
            mode: 'RANDOM',
            questions: [],
            summary: {
                // 总分数及题数
                total_score: 0,
                total_quantity: 0,
                // 各类题目的分数及题数
                judge_quantity: 0,
                judge_score: 0,
                single_quantity: 0,
                single_score: 0,
                multi_quantity: 0,
                multi_score: 0,
                fill_quantity: 0,
                fill_score: 0,
            }
        },
        created() {
            if (row && row) {
                this.mode = 'FIX'
                // $('input[name="row[mode]"][value="' + row.mode + '"]').prop('checked', true)
                // $('#row[mode]-' + row.mode).click()
                //console.log('row.mode', row.mode, this.mode)
                this.questions = row.questions
                this.statistics()
            } else {
                this.mode = 'RANDOM'
            }
        },
        methods: {
            // 修改选题模式
            changeMode(e) {
                console.log('changeMode', e.target.value)
                this.mode = e.target.value
            },

            // 打开试题选择
            openQuestionSelect() {
                console.log('openQuestionSelect')

                Fast.api.open('exam/question/select', '选择试题', {
                    area: ['90%', '90%'],
                    callback: (data) => {
                        console.log('question select callback', data)
                        for (let i = 0; i < data.length; i++) {
                            data[i].score = 1
                        }
                        // 排除已经存在的试题
                        for (let i = 0; i < this.questions.length; i++) {
                            for (let j = 0; j < data.length; j++) {
                                if (this.questions[i].id == data[j].id) {
                                    data.splice(j, 1)
                                }
                            }
                        }
                        // 合并数组
                        this.questions = this.questions.concat(data)
                        // 统计题目信息
                        this.statistics()
                    }
                })
            },

            // 修改分数
            scoreChange(index) {
                console.log('scoreChange', index)
                // 统计题目信息
                this.statistics()
            },

            // 删除试题
            deleteQuestion(index) {
                console.log('deleteQuestion', index)
                this.questions.splice(index, 1)
                // 统计题目信息
                //this.valid()

                this.statistics()
            },

            // 统计题目信息
            statistics() {
                console.log('statistics1')
                this.summary.total_score = 0
                this.summary.total_quantity = 0
                this.summary.judge_quantity = 0
                this.summary.judge_score = 0
                this.summary.single_quantity = 0
                this.summary.single_score = 0
                this.summary.multi_quantity = 0
                this.summary.multi_score = 0
                this.summary.fill_quantity = 0
                this.summary.fill_score = 0

                for (let i = 0; i < this.questions.length; i++) {
                    let question = this.questions[i]
                    this.summary.total_score += question.score
                    this.summary.total_quantity += 1
                    switch (question.kind) {
                        case 'JUDGE':
                            this.summary.judge_quantity += 1
                            this.summary.judge_score += question.score
                            break
                        case 'SINGLE':
                            this.summary.single_quantity += 1
                            this.summary.single_score += question.score
                            break
                        case 'MULTI':
                            this.summary.multi_quantity += 1
                            this.summary.multi_score += question.score
                            break
                        case 'FILL':
                            this.summary.fill_quantity += 1
                            this.summary.fill_score += question.score
                            break
                    }
                }

                let questions = []
                for (let i = 0; i < this.questions.length; i++) {
                    let question = this.questions[i]
                    questions.push({
                        id: question.id,
                        score: question.score
                    })
                }
                $('#c-questions').val(JSON.stringify(questions))

                console.log(this.questions)
            },

            // 验证
            valid() {
                var quantity = $('#c-quantity').val()
                var total_score = $('#c-total_score').val()
                console.log('trigger valid', this.mode, quantity, total_score)
                /*if (quantity == 0) {
                    Toastr.error('固定模式下，试卷题数不能为0')
                    return false
                }
                if (total_score == 0) {
                    Toastr.error('固定模式下，试卷总分不能为0')
                    return false
                }
                if (this.questions.length == 0) {
                    Toastr.error('固定模式下，试卷题目不能为空')
                    return false
                }
                if (this.summary.total_score != total_score) {
                    Toastr.error('固定模式下，试卷总分与题目总分不一致')
                    return false
                }
                if (this.summary.total_quantity != quantity) {
                    Toastr.error('固定模式下，试卷题数与题目总数不一致')
                    return false
                }*/

                // 将试题信息转换为JSON字符串
                let questions = []
                for (let i = 0; i < this.questions.length; i++) {
                    let question = this.questions[i]
                    questions.push({
                        id: question.id,
                        score: question.score
                    })
                }
                $('#c-questions').val(JSON.stringify(questions))

                valid_result = true
                return true


            }
        }
    })
</script>
<style>
    .w-60 {
        width: 60px;
    }

    .m-l-25 {
        margin-left: 25px !important;
    }

    .p-l-80 {
        margin-left: 80px !important;
    }

    .el-input-number--mini {
        width: 90px;
        line-height: 26px;
    }
</style>
